﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PhoneManager
{
    public partial class FrmProviders : FrmDefault
    {
        private class GSMProviders
        {
            public string Code { get; set; }
            public string Name { get; set; }
        }
        public FrmProviders()
        {
            InitializeComponent();
        }

        private void FrmProviders_Load(object sender, EventArgs e)
        {
            System.Collections.Specialized.StringCollection prov = Properties.Settings.Default.OperatorProviders;
            int qtd = -1;
            flowLayoutPanel.SuspendLayout();
            foreach (var item in prov)
            {
                qtd++;
                string[] itemprov = item.Split(',');
                UCProvider template = new UCProvider();
                template.EventUCProvider += new UCProvider.UCProviderEventHandler(template_EventUCProvider);
                template.Index = qtd;
                template.Code = itemprov[0];
                template.NameProvider = itemprov[1];
                template.NewProvider = false;
                flowLayoutPanel.Controls.Add(template);
                if (qtd == prov.Count-1)
                    template.AllowAdd = true;
                template.AllowDelete = true;
            }
            if (qtd == 0)
            {
                UCProvider template = new UCProvider();
                template.EventUCProvider += new UCProvider.UCProviderEventHandler(template_EventUCProvider);
                template.Index = 0;
                template.Code = "";
                template.NameProvider = "";
                template.AllowAdd = true;
                template.NewProvider = true;
                flowLayoutPanel.Controls.Add(template);
            }
            flowLayoutPanel.ResumeLayout();
        }

        void template_EventUCProvider(object sender, UCProviderEventArgs e)
        {
            switch (e.Action)
            {
                case UCProviderActionEnum.CancelAdd:
                    {
                        UCProvider CtrDel = (UCProvider)sender;
                        flowLayoutPanel.Controls.Remove(CtrDel);
                        if (flowLayoutPanel.Controls.Count == 0)
                        {
                            UCProvider template = new UCProvider();
                            template.EventUCProvider += new UCProvider.UCProviderEventHandler(template_EventUCProvider);
                            template.Index = 0;
                            template.Code = "";
                            template.NameProvider = "";
                            template.AllowAdd = true;
                            template.NewProvider = true;
                            flowLayoutPanel.Controls.Add(template);
                            template.Focus();
                        }
                        else
                        {
                            for (int i = 0; i < flowLayoutPanel.Controls.Count - 1; i++)
                            {
                                ((UCProvider)flowLayoutPanel.Controls[i]).Index = i;
                            }
                            ((UCProvider)flowLayoutPanel.Controls[flowLayoutPanel.Controls.Count - 1]).AllowAdd = true;
                            flowLayoutPanel.Controls[flowLayoutPanel.Controls.Count - 1].Focus();
                        }
                    }
                    break;
                case UCProviderActionEnum.Add:
                    {
                        UCProvider CtrAdd = (UCProvider)sender;
                        CtrAdd.AllowAdd = false;
                        UCProvider template = new UCProvider();
                        template.EventUCProvider += new UCProvider.UCProviderEventHandler(template_EventUCProvider);
                        template.Index = flowLayoutPanel.Controls.Count;
                        template.Code = "";
                        template.NameProvider = "";
                        template.NewProvider = true;
                        flowLayoutPanel.Controls.Add(template);
                        template.AllowAdd = false;
                        template.AllowDelete = true;
                        template.CodeFocus();
                    }
                    break;
                case UCProviderActionEnum.Update:
                    {
                        UCProvider CtrAdd = (UCProvider)sender;
                        string item = CtrAdd.Code.Trim() + "," + CtrAdd.NameProvider.Trim();
                        System.Collections.Specialized.StringCollection prov = Properties.Settings.Default.OperatorProviders;
                        string itemAux = CtrAdd.CodeOriginal.Trim() + "," + CtrAdd.NameProviderOriginal.Trim();
                        int index = prov.IndexOf(itemAux);
                        if (index < 0)
                        {
                            prov.Add(item);
                            CtrAdd.AllowAdd = true;
                            CtrAdd.AllowDelete = true;
                        }
                        else
                        {
                            prov[index] = itemAux;
                            CtrAdd.AllowAdd = false;
                            CtrAdd.AllowDelete = true;
                        }
                        Properties.Settings.Default.Save();
                    }
                    break;
                case UCProviderActionEnum.Remove:
                    {
                        UCProvider CtrDel = (UCProvider)sender;
                        System.Collections.Specialized.StringCollection prov = Properties.Settings.Default.OperatorProviders;
                        string item = CtrDel.Code.Trim() + "," + CtrDel.NameProvider.Trim();
                        int index = prov.IndexOf(item);
                        if (index >= 0)
                        {
                            prov.RemoveAt(index);
                        }
                        Properties.Settings.Default.Save();
                        flowLayoutPanel.Controls.Remove(CtrDel);
                        if (flowLayoutPanel.Controls.Count == 0)
                        {
                            UCProvider template = new UCProvider();
                            template.EventUCProvider += new UCProvider.UCProviderEventHandler(template_EventUCProvider);
                            template.Index = 0;
                            template.Code = "";
                            template.NameProvider = "";
                            template.AllowAdd = true;
                            flowLayoutPanel.Controls.Add(template);
                            template.Focus();
                        }
                        else
                        {
                            for (int i = 0; i < flowLayoutPanel.Controls.Count - 1; i++)
                            {
                                ((UCProvider)flowLayoutPanel.Controls[i]).Index = i;
                            }
                            ((UCProvider)flowLayoutPanel.Controls[flowLayoutPanel.Controls.Count - 1]).AllowAdd = true;
                            flowLayoutPanel.Controls[flowLayoutPanel.Controls.Count - 1].Focus();
                        }
                    }
                    break;
            }
        }
        private void BntCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
